package com.liushuai.eas.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liushuai.eas.common.Result;
import com.liushuai.eas.pojo.Building;
import com.liushuai.eas.pojo.Classes;
import com.liushuai.eas.pojo.Major;
import com.liushuai.eas.service.BuildingImp;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/building")
public class BuildingController {

    @Resource
    private BuildingImp buildingImp;

    @PostMapping
    public Result<?> add(@RequestBody Building building){

        //插入building数据库
        if (buildingImp.insert(building)!=1)
            return Result.fail(building);

        return Result.success(building);
    }

    @PutMapping
    public Result<Building> update(@RequestBody Building building){
        if (buildingImp.updateById(building)!=1)
            return Result.fail(building);
        return Result.success(building);
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        Page<Building> buildingPage = buildingImp.selectPage(new Page<Building>(pageNum, pageSize), Wrappers.<Building>lambdaQuery().like(Building::getName, search));
        return Result.success("数据加载成功",buildingPage);
    }

    @DeleteMapping
    public Result<Building> delete(@RequestBody Building building){
        if (buildingImp.deleteById(building)!=1)
            return Result.fail(building);
        return Result.success(building);
    }

    @GetMapping("/selectAll")
    public Result<List<Building>> selectAll(){
        QueryWrapper<Building> queryWrapper = new QueryWrapper<>();
        List<Building> classesList = buildingImp.selectList(queryWrapper);

        return Result.success(classesList);
    }

    @GetMapping("/selectOne")
    public Result<Building> selectOne(@RequestParam Integer buildingId){

        Building building = buildingImp.selectOne(buildingId);

        return Result.success(building);
    }

}
